	dappr.extend({
		
		/*
		 * OAuthClient - provides oauth php proxy connection
		 */ 
		oauthClient: (function(){
			var access_link = null; // OAuth authorisation URL
			var access_token = null; // OAuth access token
			var last_features = null; // last know parameters. Prevents multiple requests for the same parameters.
			
			/*
			 * Get the Authorize URL from OAuth PHP client-side proxy
			 */
			function registerFeatures$(successCB, errorCB, params) {
				if((params && 
						((params.requestedFeatures==null || params.requestedFeatures=='') || (params.requestedFeatures == last_features))) 
					|| params == null) 
						return;
					
				last_features = params.requestedFeatures; // prevents resubmission for the same parameters
				
				// contruct the request
				var requestParams = {
					action: 'register',					
					params: params
				};	
				
				var async = new dappr.asyncHandle({
					onSuccess: successCB,
					onError: ((typeof errorCB == 'function') ? errorCB : genericErrorHandler),
					timeout: 60
				});	
				async.send(requestParams);							
			}

			/*
			 * Get the status of authorised and rejected features from OAuth PHP client-side proxy
			 */
			function getStatus$() {
				// contruct the request
				var requestParams = {
					action: 'status'
				};	
				
				var async = new dappr.asyncHandle({
					onSuccess: function(json) {
						if(json.authorized!=null && dappr.isString(json.authorized)) dappr.dialog.setAuthorizedFeatures(json.authorized);
						if(json.rejected!=null && dappr.isString(json.rejected)) dappr.dialog.setRejectedFeatures(json.rejected);
					},
					onError: genericErrorHandler,
					timeout: 60
				});
				async.send(requestParams);

			}
			
			/*
			 * Perform an actual API request via the OAuth PHP client-side proxy
			 */
			function doRequest$(successCB, errorCB, params, data, timeout) {
				
				// contruct the request
				var requestParams = {
					action: 'request',
					params: params
				};
				
				var async = new dappr.asyncHandle({
					method: data ? 'POST' : 'GET',
					onSuccess: successCB,
					onError: ((typeof errorCB == 'function') ? errorCB : genericErrorHandler),
					timeout: ((timeout) ? timeout : 600)
				});	
				async.send(requestParams, data);				
			}
			
			function genericErrorHandler() {
				console.log('Error getting OAuth URL');
			};
			
			return {
				registerFeatures: function(successCB, errorCB, params) {
					registerFeatures$(successCB, errorCB, params);
				},
				
				getStatus: function() {
					getStatus$();
				},
				
				doRequest: function(successCB, errorCB, params, data, timeout) {
					doRequest$(successCB, errorCB, params, data, timeout);
				}
			};
		}())

	});
